<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Form with sessionStorage</title>
</head>

<body>
    <form id="user-form">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" value="">

        <label for="email">Email:</label>
        <input type="email" id="email" name="email" value="">

        <button type="submit">Submit</button>
    </form>

    <script>
        const form = document.getElementById('user-form');
        const usernameInput = document.getElementById('username');
        const emailInput = document.getElementById('email');

        // Restore form data from sessionStorage
        usernameInput.value = sessionStorage.getItem('username') || '';
        emailInput.value = sessionStorage.getItem('email') || '';

        // Save form data to sessionStorage on input
        form.addEventListener('input', () => {
            sessionStorage.setItem('username', usernameInput.value);
            sessionStorage.setItem('email', emailInput.value);
        });

        // Clear sessionStorage on form submit
        form.addEventListener('submit', () => {
            sessionStorage.clear();
        });
    </script>
</body>

</html>